package com.shr.code_interview;

public class Solution_35 {
    public RandomListNode Clone(RandomListNode pHead)
    {
    	if(pHead == null)
    		return null;
        RandomListNode tNode = pHead ;
        while(tNode != null){
        	RandomListNode newNode = new RandomListNode(tNode.label) ;
        	newNode.next = tNode.next ;
        	tNode.next = newNode ;
        	tNode = newNode.next ;
        }
        tNode = pHead ;
        while(tNode != null){
        	if(tNode.random != null)
        		tNode.next.random = tNode.random.next ;
        	tNode = tNode.next.next ;
        }
        tNode = pHead ;
        RandomListNode newNode = tNode.next ;
        while(tNode.next != null){
        	RandomListNode nextNode = tNode.next ;
        	tNode.next = tNode.next.next ;
        	tNode = nextNode ;
        }
        return newNode ;
    }
}
